const { ccclass, property } = cc._decorator;
import WinNodeBase from '../WinNodeBase';
import SkillBuild from '../SkillBuild/SkillBuild';
import MyEnums from '../MyEnums';
import SoundMgr from '../SoundMgr';

class Award {
    AwardType: number = 0;
    AwardValue: string = "";
}

@ccclass
export default class GameOverControl extends WinNodeBase {
    @property(cc.Node)
    scoreNode: cc.Node = null;
    @property(cc.Node)
    msgNode: cc.Node = null;
    @property(cc.Node)
    awardNode: cc.Node = null;
    @property(cc.Node)
    skillBilder: cc.Node = null;
    @property(cc.Node)
    SoundNode: cc.Node = null;

    @property(cc.Label)
    jishaNode: cc.Label = null;
    @property(cc.Label)
    shoushangNode: cc.Label = null;
    @property(cc.Label)
    haoshiNode: cc.Label = null;
    @property(cc.String)
    spritePath: string = "gameOver";
    @property(cc.String)
    prefabPath: string = "prizesItem";

    _RandomAward: boolean = false;
    _spriteAtlas: cc.SpriteAtlas = null;
    _score: number = 0;
    _isWin: boolean = false;
    _isFirstWin: boolean = false;
    _ScoreAward: Award[] = [];
    _startNode: cc.Node = null;
    _resultNode: cc.Node = null;
    _prefabAtlas: cc.Prefab = null;
    _skillBilder: SkillBuild = null;
    _dbSkill: any = null;
    _dbGrade: any = null;
    _SoundMgr: SoundMgr = null;

    expRandom: string[] = ["100", "150", "200", "400"];
    ftpRandom: string[] = ["5", "5", "10", "15"];
    moneyRandom: string[] = ["5", "10", "10", "15"];

    onLoad() {
        this._skillBilder = this.skillBilder.getComponent(SkillBuild);
        let heroH = gameHelper.getInstance();
        this._dbSkill = heroH.skillParam;
        this._dbGrade = heroH.gradeParam;
        this._SoundMgr = this.SoundNode.getComponent(SoundMgr);
        this.loadSpriteAtlas();
    }

    loadSpriteAtlas() {
        let self = this;
        cc.resources.load(this.spritePath, cc.SpriteAtlas, function (err, atlas) {
            if (err) {
                cc.log(err);
                return;
            }
            self._spriteAtlas = (atlas as cc.SpriteAtlas);
            self.setUI();
        });
    }

    private setUI() {
        this.node.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("bg3");
        this.setScore();
        this.setMsg();
        this.setPrizes();
    }

    private setScore() {
        this.scoreNode.active = true;
        this._startNode = this.scoreNode.getChildByName("star");
        this._resultNode = this.scoreNode.getChildByName("result");
        for (let index = 0; index < this._score; index++) {
            this._startNode.children[index].active = true;
            this._startNode.children[index].getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("star");
        }
        if (this._isWin) {
            this._resultNode.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("win");
            this._SoundMgr.play("_gamewin");
        }
        else {
            this._resultNode.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("fail");
            this._SoundMgr.play("_gameover");
        }
    }
    private setMsg() {
        this.msgNode.active = true;
    }
    //游戏奖励
    private setPrizes() {
        let self = this;
        this.awardNode.active = true;
        let prizesLable = this.awardNode.getChildByName("prizesLable");
        prizesLable.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("prizes");
        let prizesList = this.awardNode.getChildByName("prizesList");
        prizesList.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("bg2");

        cc.resources.load(this.prefabPath, cc.Prefab, function (err, prefab) {
            if (err) {
                cc.log(err);
                return;
            }
            self._prefabAtlas = (prefab as cc.Prefab);
            self._ScoreAward.forEach(element => {
                prizesList.addChild(self.loadAward(element))
            }, self);
            if (self._RandomAward) {
                let rand = Math.floor(Math.random() * 4);
                let element = { AwardType: rand, AwardValue: null };
                prizesList.addChild(self.loadAward(element));
            }
            let heroH = gameHelper.getInstance();
            heroH.skillParam = self._dbSkill;
            heroH.gradeParam = self._dbGrade;
        });
    }
    private loadAward(award: Award): cc.Node {
        let node = cc.instantiate(this._prefabAtlas);
        let label = "";
        switch (award.AwardType) {
            case MyEnums.AwardType.exp:
                node.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("exp");
                if (!award.AwardValue) {
                    let rand = Math.floor(Math.random() * 4);
                    award.AwardValue = this.expRandom[rand];
                }
                label = award.AwardValue;
                this._dbGrade.exp += Number.parseInt(award.AwardValue);
                break;
            case MyEnums.AwardType.ftp:
                node.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("ftg");
                if (!award.AwardValue) {
                    let rand = Math.floor(Math.random() * 4);
                    award.AwardValue = this.ftpRandom[rand];
                }
                label = award.AwardValue;
                this._dbGrade.ftg += Number.parseInt(award.AwardValue);
                break;
            case MyEnums.AwardType.money:
                node.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("money");
                if (!award.AwardValue) {
                    let rand = Math.floor(Math.random() * 4);
                    award.AwardValue = this.moneyRandom[rand];
                }
                label = award.AwardValue;
                this._dbGrade.money += Number.parseInt(award.AwardValue);
                break;
            case MyEnums.AwardType.skill:
                node.getComponent(cc.Sprite).spriteFrame = this._spriteAtlas.getSpriteFrame("skill");
                if (!award.AwardValue) {
                    label = "未鉴定秘籍"
                }
                else {
                    label = this._skillBilder._SkillKeyMap.get(award.AwardValue);
                }
                this._dbSkill.b.push(award.AwardValue);
                this._dbSkill.new = true;
                break;
        };
        node.getChildByName("Label").getComponent(cc.Label).string = label;
        return node;
    }
}
